//
//  MapDestinationViewController.m
//  LZMPro
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 LZS. All rights reserved.
//

#import "MapDestinationViewController.h"
#import "AFNData.h"
#import "MapDesElementsModel.h"
#import "MapDesCItyModel.h"
#import "MapDesCollectionReusableHeaderView.h"
#import "MapDesCollectionViewCell.h"
#import "MapDestinationDetailsViewController.h"
#import "MapCityViewController.h"


@interface MapDestinationViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong)  BaseCollectionView *myColletionView;

@end

@implementation MapDestinationViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = NO;
    
    self.navigationController.navigationBar.barTintColor = MBCOLOR;
    [[[self.navigationController.navigationBar subviews]objectAtIndex:0] setAlpha:1];
    
}


-(NSString *)getdataPath
{
    
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *str = [array lastObject];
    
    str  = [str stringByAppendingPathComponent:@"mapDestinationDB"];
    
    return str;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    self.dataArray = [NSMutableArray array];
    
    [self GetAFNData];
    
    //懒加载调用
    [self.view addSubview:self.myColletionView];
    self.myColletionView.backgroundColor = [UIColor whiteColor];
    
}

-(BaseCollectionView *)myColletionView
{
    if (_myColletionView==nil) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        
        flowLayout.itemSize = CGSizeMake(WIDTH * 0.44 , WIDTH * 0.44);
        
        flowLayout.minimumLineSpacing = 5;
        flowLayout.minimumInteritemSpacing = 5;
        flowLayout.headerReferenceSize = CGSizeMake(WIDTH, HEIGHT * 0.07);
        
        flowLayout.sectionInset = UIEdgeInsetsMake(0,WIDTH * 0.05, 0, WIDTH * 0.05);
        
        
        _myColletionView = [[BaseCollectionView alloc] initWithFrame: CGRectMake(0,0, WIDTH, HEIGHT - 113)collectionViewLayout:flowLayout];
        _myColletionView.bounces = NO;
        _myColletionView.delegate = self;
        _myColletionView.dataSource = self;
        
        [_myColletionView registerClass:[MapDesCollectionViewCell class] forCellWithReuseIdentifier:@"DestinationCell"];
        
        [_myColletionView registerClass:[MapDesCollectionReusableHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"destinationHeader"];
        

        
    }
    
    return _myColletionView;
    
    
  }


-(void)dealwtihData:(id)responseObject
{
    NSArray *array  = [responseObject objectForKey:@"elements"];
    self.dataArray =  [NSMutableArray arrayWithArray:array];
    for (int i =0 ; i < self.dataArray.count ; i++ ) {
        
        MapDesElementsModel *model = [[MapDesElementsModel alloc]init];
        NSDictionary *dic = self.dataArray[i];
        [model setValuesForKeysWithDictionary:dic];
        [self.dataArray replaceObjectAtIndex:i withObject:model];
        
    }
    
    [self.myColletionView reloadData];

}

-(void)GetAFNData
{
    
    NSString *str = @"http://api.breadtrip.com/destination/v3/";
    
    [AFNData getAFNDataWithUrl:str success:^(id responseObject) {
        
        [self dealwtihData:responseObject];
        NSDictionary *dic = responseObject;
        
        [dic writeToFile:[self getdataPath] atomically:YES];
        
        
        
        
    } failure:^(NSError *error) {
        
        NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:[self getdataPath]];
        
        [self dealwtihData:dic];
        
        
    }];
    
    
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    MapDesElementsModel *model = self.dataArray[section];
    
    
    return model.data.count;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.dataArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MapDesCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DestinationCell" forIndexPath:indexPath];
    
    
    MapDesElementsModel *m = self.dataArray[indexPath.section];
    
    MapDesCItyModel *model = m.data[indexPath.row];
    cell.model = model;
    
    
    return cell;
    
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;
    
    if (kind == UICollectionElementKindSectionHeader){
        
        MapDesCollectionReusableHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"destinationHeader" forIndexPath:indexPath];
        
        reusableview = headerView;
        headerView.model = self.dataArray[indexPath.section];
        
    }
    
    return reusableview;

}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    NSString *urlStr;
    NSString *type;
    NSString *imageUrl;
    
    MapDesElementsModel *m = [self.dataArray objectAtIndex:indexPath.section];
    
    MapDesCItyModel *model = m.data[indexPath.row];
    
    
    
    
    
    urlStr = model.ID;
    type = model.type;
    imageUrl = model.cover;
    
    if ([type integerValue] != 5) {
         MapDestinationDetailsViewController *detailVC = [[MapDestinationDetailsViewController alloc]init];
        detailVC.imageUrl = imageUrl;
        detailVC.UrlStr = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@", type, urlStr];
        [self.navigationController pushViewController:detailVC animated:YES];
        
    }
    else{
        
        MapCityViewController *city = [[MapCityViewController alloc]init];
        city.UrlStr = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/5/%@", urlStr];
        city.model = model;
        [self.navigationController pushViewController:city animated:YES];
        
    }

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
